#include "Alice/Graphs/Brush.h"
#include "glad/glad.h"
#include "GLFW/glfw3.h"
#include "Alice/Window/WindowMngr.h"
#include "Alice/Resource/Shader.h"
#include "Alice/Log/Log.h"
#include "Alice/Math/Vec2.h"

namespace Alice
{

void Brush::Init()
{
    glGenVertexArrays(1, &m_VAO);
    glGenBuffers(1, &m_VBO);
}

void Brush::DrawRect(WindowInfo& window_info, float x, float y, float w, float h, float angle)
{
    Vec2 left_top(x, y);
    Vec2 left_bottom(x, y + h);
    Vec2 right_top(x + w, y);
    Vec2 right_bottom(x + w, y + h);
    Vec2 center{x + w / 2, y + h / 2};

    // 旋转矩形
    left_top = Vec2::CalRotateDot(left_top, center, angle);
    right_top = Vec2::CalRotateDot(right_top, center, angle);
    left_bottom = Vec2::CalRotateDot(left_bottom, center, angle);
    right_bottom = Vec2::CalRotateDot(right_bottom, center, angle);

    Normalized(left_top, window_info.width, window_info.height);
    Normalized(left_bottom, window_info.width, window_info.height);
    Normalized(right_top, window_info.width, window_info.height);
    Normalized(right_bottom, window_info.width, window_info.height);

    float left_top_x = left_top.x;
    float left_top_y = left_top.y;

    float right_top_x = right_top.x;
    float right_top_y = right_top.y;
    
    float left_bottom_x = left_bottom.x;
    float left_bottom_y = left_bottom.y;

    float right_bottom_x = right_bottom.x;
    float right_bottom_y = right_bottom.y;

    float vertices[] = {
        left_top_x, left_top_y,
        left_bottom_x, left_bottom_y,
        right_top_x, right_top_y,

        left_bottom_x, left_bottom_y,
        right_top_x, right_top_y,
        right_bottom_x, right_bottom_y
    };

    glBindVertexArray(m_VAO);
    
    glBindBuffer(GL_ARRAY_BUFFER, m_VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (float*)0);
    // 解绑 VBO
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    // 使用着色器绘制
    m_shader.Use();
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    glDrawArrays(GL_TRIANGLES, 0, 6);
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    // 解绑 VAO
    glBindVertexArray(0);
}

void Brush::Normalized(Vec2& dot, float width, float height)
{
    float half_width = width / 2;
    float half_height = height / 2;

    if (dot.x == half_width && dot.y == half_height)
    {  
        dot.x = 0.0f;
        dot.y = 0.0f;
    }
    else if (dot.x == half_width && dot.y != half_height)
    {
        dot.x = 0.0f;
        if (dot.y < half_height)
            dot.y = (half_height - dot.y) / half_height;
        else
            dot.y = -((dot.y - half_height) / half_height);
    }
    else if (dot.x != half_width && dot.y == half_height)
    {
        dot.y = 0.0f;
        if (dot.x < half_width)
            dot.x = -(1.0f - (dot.x / half_width));
        else
            dot.x = (dot.x - half_width) / half_width;
    }
    // 第一象限
    else if (dot.x > half_width && dot.y < half_height)
    {
        dot.x = (dot.x - half_width) / half_width;
        dot.y = (half_height - dot.y) / half_height;
    }
    // 第二象限
    else if (dot.x < half_width && dot.y < half_height)
    {
        dot.x = -(1.0f - (dot.x / half_width));
        dot.y = (half_height - dot.y) / half_height;
    }
    // 第三象限
    else if (dot.x < half_width && dot.y > half_height)
    {
        dot.x = -(1.0f - (dot.x / half_width));
        dot.y = -((dot.y - half_height) / half_height);
    }
    // 第四象限
    else if (dot.x > half_width && dot.y > half_height)
    {
        dot.x = (dot.x - half_width) / half_width;
        dot.y = -((dot.y - half_height) / half_height);
    }
}

void Brush::Destroy()
{
    glDeleteBuffers(1, &m_VBO);
    glDeleteVertexArrays(1, &m_VAO);
}

} // namespace Alice
